(function($){
	$.fn.tabControl = function(ops){
		ops = $.extend({
			hand:'.item',
			handleType:'mouseenter',
			delay:true,
			onOpenBefore:null,
			onOpen:null,
			onOpenAfter:null,
			handCurr:'current',
			targetCurr:'active',
			btn:null,
			auto:false,
			interval:3000,
			animate:null,
			fn : false
		},ops);

		var self = $(this),
			hands = self.find(ops.hand),
			flag = $('.'+ops.handCurr,hands).index(),
			oldEle = $('.'+ops.handCurr,self),
			length = hands.length,
			timer,
			timeout,
			auto = {
				set : function(callback){
					if(ops.auto){
						timer = setInterval(function(){
							callback();
						},ops.interval);
					}
				},
				remove : function(){
					if(ops.auto){
						clearInterval(timer);
					}
				}
			}
		function handler(ele){

			var hand = $(ele),
				target = function(){
					if(!ops.fn){
							return hand.attr('data-for');
					}

					var tabParent = hand.parents(ops.fn);
					var targets = tabParent.find('.fn-tab-child');
					return targets.eq(function(){
						var _index;
						hand.parent().find(ops.hand).each(function(index,that){
							if(that == hand[0]){
								_index = index;
							}
						})
						return _index;
					}())

				}();
			if(target && !ops.fn){
				target = $('#'+target);
			}else if(target){
				target = target;
			}else{
				return;
			}
			flag = function(){
				var _index;
				hand.parent().find(ops.hand).each(function(index,that){
					if(that == hand[0]){
						_index = index;
					}
				})
				return _index;
			}();

			if(ops.auto){
				auto.remove();
			}
			if(ops.delay && ops.handleType==='mouseenter'){
				timeout = setTimeout(function(){
					if(timeout)
						clearTimeout(timeout);
					if(ops.onOpenBefore)
						ops.onOpenBefore(hand,target,oldEle);

					hand.parent().find('.'+ops.handCurr).removeClass(ops.handCurr);
					hand.addClass(ops.handCurr);

					target.parent().find('.'+ops.targetCurr).removeClass(ops.targetCurr);
					target.addClass(ops.targetCurr);
					oldEle = $('.'+ops.handCurr,self);

					if(ops.onOpen)
						ops.onOpen(hand,target);
					if(ops.onOpenAfter)
						ops.onOpenAfter(hand,target,oldEle);
				},ops.delay|150);

			}else{
				if(ops.onOpenBefore)
					ops.onOpenBefore(hand,target,oldEle);

				hand.parent().find('.'+ops.handCurr).removeClass(ops.handCurr);
				hand.addClass(ops.handCurr);
				target.parent().find('.'+ops.targetCurr).removeClass(ops.targetCurr);
				target.addClass(ops.targetCurr);
				oldEle = $('.'+ops.handCurr,self);

				if(ops.onOpen)
					ops.onOpen(hand,target);
				if(ops.onOpenAfter)
					ops.onOpenAfter(hand,target,oldEle);

			}

			if(ops.auto){
				auto.set(function(){
					next.click();
				});
			}

		}
		hands.on(ops.handleType,function(e){
			handler(this);
		});
		if(ops.btn){
			var
				prve = self.find('[prve=true]'),
				next = self.find('[next=true]');

			prve.click(function(){
				if(flag == 0){
					handler(hands[length - 1]);
				}else{
					handler(hands[flag-1]);
				}

			})
			next.click(function(){
				if(flag == length - 1){
					handler(hands[0]);
				}else{
					handler(hands[flag+1]);
				}



			})
		}

		if(ops.auto){
			auto.set(function(){
				next.click();
			});
		}

		if(ops.delay && ops.handleType==='mouseenter')
			hands.on('mouseleave',function(e){
				if(timeout)
					clearTimeout(timeout);
			})

		return this;

	}

})(jQuery);
